﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_FlatManger_frm_ParameterSet : System.Web.UI.Page
{
    public void btnSetClick(object sender, EventArgs e)
    {
        int Command = Int32.Parse(((Button)sender).CommandName);

        ClsSystemSetDAL cls_SystemSet = new ClsSystemSetDAL();
        Boolean is_succeed = false;
        bool is_set1, is_set2;
        string strCount1, strCount2;
        int set_type1, set_type2;

        switch (Command)
        { 
            case 1:
                is_set1 = !this.chkSet1.Checked;
                is_set2 = !this.chkSet2.Checked;
                strCount1 = this.txtSet1.Text.Trim();
                strCount2 = this.txtSet2.Text.Trim();
                set_type1 = this.dropSet1.SelectedIndex;
                set_type2 = this.dropSet2.SelectedIndex;
                is_succeed = cls_SystemSet.UpdateSystemSet(Command, is_set1, is_set2, strCount1, strCount2, set_type1, set_type2);
                break;
            case 2:
                is_set1 = !this.chkSet3.Checked;
                is_set2 = !this.chkSet4.Checked;
                strCount1 = this.txtSet3.Text.Trim();
                strCount2 = this.txtSet4.Text.Trim();
                set_type1 = this.dropSet3.SelectedIndex;
                set_type2 = this.dropSet4.SelectedIndex;
                is_succeed = cls_SystemSet.UpdateSystemSet(Command, is_set1, is_set2, strCount1, strCount2, set_type1, set_type2);
                break;
        }

        this.lblJs.Text = (is_succeed) ? "<script>alert('保存参数设置成功！');</script>" : "<script>alert('保存参数设置失败，请重试！');</script>";
    }

    public void btnResultClick(object sender, EventArgs e)
    {
        int Command = Int32.Parse(((Button)sender).CommandName);

        this.SetParam(Command);
    }

    public void SetParamType(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            int Command = Int32.Parse(((ImageButton)sender).CommandName);

            this.SetParam(Command);
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void CheckedChanged1(object sender, EventArgs e)
    {
        CheckBox chkBox = (CheckBox)sender;

        if (chkBox.Checked)
        {
            if (chkBox.ID == "chkSet1")
            {
                this.txtSet1.Enabled = this.dropSet1.Enabled = false;
            }
            else
            {
                this.txtSet2.Enabled = this.dropSet2.Enabled = false;
            }
        }
        else
        {
            if (chkBox.ID == "chkSet1")
            {
                this.txtSet1.Enabled = this.dropSet1.Enabled = true;
            }
            else
            {
                this.txtSet2.Enabled = this.dropSet2.Enabled = true;
            }
        }
    }

    public void CheckedChanged2(object sender, EventArgs e)
    {
        CheckBox chkBox = (CheckBox)sender;

        if (chkBox.Checked)
        {
            if (chkBox.ID == "chkSet3")
            {
                this.txtSet3.Enabled = this.dropSet3.Enabled = false;
            }
            else
            {
                this.txtSet4.Enabled = this.dropSet4.Enabled = false;
            }
        }
        else
        {
            if (chkBox.ID == "chkSet3")
            {
                this.txtSet3.Enabled = this.dropSet3.Enabled = true;
            }
            else
            {
                this.txtSet4.Enabled = this.dropSet4.Enabled = true;
            }
        }
    }

    private void SetParam(int Command)
    {
        ClsSystemSetDAL cls_SystemSet = new ClsSystemSetDAL();
        DataSet dst = cls_SystemSet.GetSystemParamSetDataSet(Command);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.ibtnSet1.ImageUrl = "../App_Images/b_51_2.gif";
            this.ibtnSet2.ImageUrl = "../App_Images/b_51_4.gif";
            this.ibtnSet3.ImageUrl = "../App_Images/b_51_6.gif";
            this.ibtnSet4.ImageUrl = "../App_Images/b_51_8.gif";
            this.ibtnSet5.ImageUrl = "../App_Images/b_51_10.gif";
            this.panSet1.Visible = false;
            this.panSet2.Visible = false;
            this.panSet3.Visible = false;
            this.panSet4.Visible = false;
            this.panSet5.Visible = false;

            bool is_set1 = bool.Parse(dst.Tables[0].Rows[0][2].ToString());
            bool is_set2 = bool.Parse(dst.Tables[0].Rows[0][5].ToString());
            string strCount1 = dst.Tables[0].Rows[0][3].ToString();
            string strCount2 = dst.Tables[0].Rows[0][6].ToString();
            int set_type1 = Int32.Parse(dst.Tables[0].Rows[0][4].ToString());
            int set_type2 = Int32.Parse(dst.Tables[0].Rows[0][7].ToString());

            switch (Command)
            {
                case 1:
                    this.ibtnSet1.ImageUrl = "../App_Images/b_51_1.gif";
                    this.panSet1.Visible = true;
                    this.chkSet1.Checked = !is_set1;
                    this.chkSet2.Checked = !is_set2;
                    this.txtSet1.Text = strCount1;
                    this.txtSet2.Text = strCount2;
                    this.dropSet1.SelectedIndex = set_type1;
                    this.dropSet2.SelectedIndex = set_type2;
                    this.txtSet1.Enabled = this.dropSet1.Enabled = is_set1;
                    this.txtSet2.Enabled = this.dropSet2.Enabled = is_set2;
                    break;
                case 2:
                    this.ibtnSet2.ImageUrl = "../App_Images/b_51_3.gif";
                    this.panSet2.Visible = true;
                    this.chkSet3.Checked = !is_set1;
                    this.chkSet4.Checked = !is_set2;
                    this.txtSet3.Text = strCount1;
                    this.txtSet4.Text = strCount2;
                    this.dropSet3.SelectedIndex = set_type1;
                    this.dropSet4.SelectedIndex = set_type2;
                    this.txtSet3.Enabled = this.dropSet3.Enabled = is_set1;
                    this.txtSet4.Enabled = this.dropSet4.Enabled = is_set2;
                    break;
                case 3:
                    this.ibtnSet3.ImageUrl = "../App_Images/b_51_5.gif";
                    this.panSet3.Visible = true;
                    break;
                case 4:
                    this.ibtnSet4.ImageUrl = "../App_Images/b_51_7.gif";
                    this.panSet4.Visible = true;
                    break;
                default:
                    this.ibtnSet5.ImageUrl = "../App_Images/b_51_9.gif";
                    this.panSet5.Visible = true;
                    break;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ClsSystemSetDAL cls_SystemSet = new ClsSystemSetDAL();
                DataSet dst = cls_SystemSet.GetSystemParamSetDataSet(1);

                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    bool is_set1 = bool.Parse(dst.Tables[0].Rows[0][2].ToString());
                    bool is_set2 = bool.Parse(dst.Tables[0].Rows[0][5].ToString());
                    string strCount1 = dst.Tables[0].Rows[0][3].ToString();
                    string strCount2 = dst.Tables[0].Rows[0][6].ToString();
                    int set_type1 = Int32.Parse(dst.Tables[0].Rows[0][4].ToString());
                    int set_type2 = Int32.Parse(dst.Tables[0].Rows[0][7].ToString());

                    this.chkSet1.Checked = !is_set1;
                    this.chkSet2.Checked = !is_set2;
                    this.txtSet1.Text = strCount1;
                    this.txtSet2.Text = strCount2;
                    this.dropSet1.SelectedIndex = set_type1;
                    this.dropSet2.SelectedIndex = set_type2;
                    this.txtSet1.Enabled = this.dropSet1.Enabled = is_set1;
                    this.txtSet2.Enabled = this.dropSet2.Enabled = is_set2;
                }
            }
            else
            {
                this.lblJs.Text = "";
            }

            this.txtSet1.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            this.txtSet2.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            this.txtSet3.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            this.txtSet4.Attributes.Add("onkeyup", "value=value.replace(/[^\\d]/g,'')");
            this.txtSet1.Attributes.Add("onblur", "if(value.Trim() == '') {alert('小数位数不能为空！'); this.focus();}");
            this.txtSet2.Attributes.Add("onblur", "if(value.Trim() == '') {alert('小数位数不能为空！'); this.focus();}");
            this.txtSet3.Attributes.Add("onblur", "if(value.Trim() == '') {alert('小数位数不能为空！'); this.focus();}");
            this.txtSet4.Attributes.Add("onblur", "if(value.Trim() == '') {alert('小数位数不能为空！'); this.focus();}");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }
}
